package com.b1.controller;

import cn.hutool.core.bean.BeanUtil;
import com.b1.common.BaseResponse;
import com.b1.common.DeleteRequest;
import com.b1.common.ErrorCode;
import com.b1.common.ResultUtils;
import com.b1.exception.BusinessException;
import com.b1.exception.ThrowUtils;
import com.b1.pojo.dto.childernSleepRecord.ChildernSleepAddRequest;
import com.b1.pojo.dto.childernSleepRecord.ChildernSleepQueryRequest;
import com.b1.pojo.enity.ChildernSleepRecord;
import com.b1.service.ChildernSleepRecordService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("b1/childrenSleepRecord")
@CrossOrigin
@Api(tags = "这是用来管理每个班级儿童的午睡情况的记录的接口")
@Slf4j
public class ChildernSleepRecordController {
    @Resource
    private ChildernSleepRecordService childernSleepRecordService;



    @PostMapping("/add")
    @ApiOperation(value = "添加午睡记录")
    public BaseResponse<Integer> addChildernSleepRecord(@RequestBody ChildernSleepAddRequest childernSleepAddRequest){
        if (childernSleepAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ChildernSleepRecord childernSleepRecord = new ChildernSleepRecord();
        BeanUtil.copyProperties(childernSleepAddRequest, childernSleepRecord);
        childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, true);
        boolean result = childernSleepRecordService.save(childernSleepRecord);
        ThrowUtils.throwIf(!result ,ErrorCode.OPERATION_ERROR, "添加失败");
        Integer recordId = childernSleepRecord.getId();
        return ResultUtils.success(recordId);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除午睡记录")
    public BaseResponse<Boolean> deleteChildernSleepRecord(@RequestBody DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long id = deleteRequest.getId();
        // 判断是否存在
        ChildernSleepRecord oldChildernSleepRecord = childernSleepRecordService.getById(id);
        ThrowUtils.throwIf(oldChildernSleepRecord == null, ErrorCode.NOT_FOUND_ERROR);
        boolean b = childernSleepRecordService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新（仅管理员）
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "更新午睡记录")
    public BaseResponse<Boolean> updatePost(@RequestBody  ChildernSleepRecord childernSleepRecord) {
        if (childernSleepRecord == null || childernSleepRecord.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 参数校验
        childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, false);
        long id = childernSleepRecord.getId();
        // 判断是否存在
        ChildernSleepRecord oldChildernSleepRecord = childernSleepRecordService.getById(id);
        ThrowUtils.throwIf(oldChildernSleepRecord == null, ErrorCode.NOT_FOUND_ERROR);
        boolean result = childernSleepRecordService.updateById(childernSleepRecord);
        return ResultUtils.success(result);
    }

    /**
     * 分页获取列表（仅管理员）
     * @return
     */
    @PostMapping("/list/page")
    @ApiOperation(value = "分页获取午睡记录")
    public BaseResponse<Page<ChildernSleepRecord>> listPostByPage(@RequestBody ChildernSleepQueryRequest childernSleepQueryRequest) {
        long current = childernSleepQueryRequest.getCurrent();
        long size = childernSleepQueryRequest.getPageSize();
        Page<ChildernSleepRecord> childernSleepPage = childernSleepRecordService.page(new Page<>(current, size),
                childernSleepRecordService.getQueryWrapper(childernSleepQueryRequest));
        return ResultUtils.success(childernSleepPage);
    }



    /**
     * 根据id获取
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "根据id获取午睡记录")
    public BaseResponse<ChildernSleepRecord> getPost(@RequestParam("id") Long id) {
             if (id == null || id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        ChildernSleepRecord childernSleepRecord = childernSleepRecordService.getById(id);
        return ResultUtils.success(childernSleepRecord);
    }

    @PostMapping("/list")
    @ApiOperation(value = "获取所有的幼儿园")
    public BaseResponse<List<String>> listPost(){
        List<ChildernSleepRecord> list = childernSleepRecordService.list();
        if (list == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "没有找到幼儿园");
        }
        Set<String> set = new HashSet<>();
        for (ChildernSleepRecord childernSleepRecord : list) {
            set.add(childernSleepRecord.getKindName());
        }
        List<String> list1 = new ArrayList<>(set);
        Collections.sort(list1);
        return ResultUtils.success(list1);
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出午睡记录")
    public BaseResponse<String> export(@RequestBody Long[] ids) throws IOException {
        return ResultUtils.success(childernSleepRecordService.export(ids));

    }

    //批量修改
    @PostMapping("/update/batch")
    @ApiOperation(value = "批量修改午睡记录")
    public BaseResponse<Boolean> updateBatch(@RequestBody List<ChildernSleepRecord> childernSleepRecords){
        //如果修改失败返回哪些数据修改失败
        for (ChildernSleepRecord childernSleepRecord : childernSleepRecords) {
            if(childernSleepRecord == null){
                throw new BusinessException(ErrorCode.PARAMS_ERROR,"传入的修改记录不能为空");
            }
            childernSleepRecordService.validChildernSleepRecord(childernSleepRecord, false);
            //判断改数据是否存在
            ChildernSleepRecord oldChildernSleepRecord = childernSleepRecordService.getById(childernSleepRecord.getId());
            if(oldChildernSleepRecord == null){
                throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"传入的修改记录不存在");
            }
        }
        return ResultUtils.success(childernSleepRecordService.updateBatch(childernSleepRecords));
    }



}
